package web

import (
	"context"

	"portal/internal/errorx"
	"portal/internal/svc"
	"portal/internal/types"
	"portal/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListJobHanderLogic struct {
	logx.Logger
	ctx      context.Context
	svcCtx   *svc.ServiceContext
	jobmodel model.AwJobsModel
}

func NewListJobHanderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListJobHanderLogic {
	return &ListJobHanderLogic{
		Logger:   logx.WithContext(ctx),
		ctx:      ctx,
		svcCtx:   svcCtx,
		jobmodel: model.NewAwJobsModel(svcCtx.Orm),
	}
}

func (l *ListJobHanderLogic) ListJobHander(req *types.ListJobRequest) (resp *types.CommonResponse, err error) {
	// todo: add your logic here and delete this line
	jobDtos, err := l.jobmodel.ListByCondition(l.ctx, nil, req.PageNum, req.PageSize, req.CategoryIds, req.CityIds,req.Keyword)
	if err != nil {
		return nil, errorx.NewBizError(500, err.Error())
	}
	jobVos := make([]*types.JobVo, 0)
	for _, job := range jobDtos {
		jobVos = append(jobVos, &types.JobVo{
			Id:          job.Id,
			JobCategory: job.JobCategory.Int64,
			Description: job.Description,
			Requirement: job.Requirement,
			City:        job.City.Int64,
			CreateTime:  job.CreateTime,
			UpdateTime:  job.UpdateTime,
			Title:       job.Title,
			Catename:    job.Catename,
			Cityname:    job.Cityname,
		})
	}
	total, err := l.jobmodel.GetCount(l.ctx, nil,req.Keyword, req.CategoryIds, req.CityIds)
	if err != nil {
		return nil, errorx.NewBizError(500, err.Error())
	}
	return &types.CommonResponse{
		Status: 200,
		Msg:    "查询成功",
		Data: &types.ListJobResponse{
			Jobs: jobVos,
			PageParamResponse: types.PageParamResponse{
				Total: total,
				Size:  int64(len(jobVos)),
			},
		},
	}, nil
}
